#include"head.h"
#include"funcs.h"
#include"nodes.h"

extern unordered_map<char, node*> buf;
extern unordered_map<char, int> dic;

void analyse() {
	ifstream in(sourceFile);
	vector<string> sorce;
	stack<int> tar;
	stack<char> ana;

	ana.push('S');
	tar.push('#');

	while (!in.eof()) {
		string temp;
		in >> temp;
		sorce.push_back(temp);
	}

	sorce.pop_back();

	for (int i = sorce.size() - 1; i >= 0; i--) {
		tar.push(toInt(sorce[i][sorce[i].size()- 2]));
	}

	while (tar.top()!='#') {
		char analy = ana.top();
		ana.pop();
		

		if (isEnding(analy)) {
			if (dic[analy] == tar.top()) {
				tar.pop();
				continue;
			}
			exit(-1);
		}
		auto flag = buf[analy]->firstSets.find(tar.top());
		if (flag == buf[analy]->firstSets.end()) {
			auto flag2 = buf[analy]->followSets.find(tar.top());
			if (flag2 == buf[analy]->followSets.end()) {
				exit(-1);
			}
		}
		else {
			vector<string> temp = split(buf[analy]->prods, '|');
			if (isEnding(temp[0][0])) {
				if (dic[temp[0][0]] == tar.top()) pushToStack(temp[0], ana);
				else pushToStack(temp[1], ana);

				continue;
			}
			auto flag2 = buf[temp[0][0]]->firstSets.find(tar.top());
			if (flag2 != buf[temp[0][0]]->firstSets.end()) pushToStack(temp[0], ana);
			else pushToStack(temp[1], ana);
			//tar.pop();
		}

	}

	cout << "success";
}